﻿using BLL;
using Common;
using DevExpress.XtraEditors;
using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class HealthEduPrint : XtraForm
    {
        protected string path = "";
        private GridppReport Report = new GridppReport();
        public NursingFormSystem.DNNursingService.BNNursingDocsSystem bnService = new NursingFormSystem.DNNursingService.BNNursingDocsSystem();
        public static Tb_User LoginUser;
        public PatientInfoView PatientParams;
        public object printer;
        public string printdefaultsql = "";
        ExeConfigurationFileMap map = new ExeConfigurationFileMap();//读配置文件的
        public HealthEduPrint(string sql)
        {
            InitializeComponent();
            string str = Application.StartupPath;
            map.ExeConfigFilename = str + "\\Resources\\NursingFormSystem.config";
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            path = config.AppSettings.Settings["Hospital"].Value.ToString();//医院名称
            //设置 ShowPrintDlg 属性，让点击工具栏中的打印按钮时不显示打印对话框
            axGRPrintViewer1.ShowPrintDlg = false;
            string printdefaultsql = @"SELECT [PrinterName]  FROM [BNNursingDocsSystem_Develop].[dbo].[PrintDefault](NOLOCK) where FromName  = '" + Kind.getFileName() + "' and MACAddress = '" + Kind.GetMacAddress() + "'";
            printer = SqlHelper.ExecuteScalar(CommandType.Text, printdefaultsql);
            if (printer == null)  //没设置过打印机
            {
                XtraMessageBox.Show("此页面，该MAC地址未设置过默认打印机，请预览时设置默认打印机，系统将自动保存！");
            }
            else
            {
                Report.Printer.PrinterName = printer.ToString();
            }
            try
            {
                if (File.Exists(Application.StartupPath + @"\GRFModel\Ydhl_GRF\HealthEduPrint.grf"))
                {
                    Report.LoadFromFile(Application.StartupPath + @"\GRFModel\Ydhl_GRF\HealthEduPrint.grf");
                }
                else
                {
                    Report.LoadFromFile(@"../../Ydhl_GRF/HealthEduPrint.grf");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("加载健康宣教表单失败，请检查文件Ydhl_GRF/HealthEduPrint.grf是否存在");
                WriteLog.WriteLogString(ex.Message + ".  加载健康宣教表单失败，请检查文件Ydhl_GRF/HealthEduPrint.grf是否存在");
                return;
            }
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.DetailGrid.Recordset.QuerySQL = sql;
            axGRPrintViewer1.Report = Report;
            axGRPrintViewer1.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
        
        }
        private void axGRPrintViewer1_CustomButtonClick_1(object sender, Axgregn6Lib._IGRPrintViewerEvents_CustomButtonClickEvent e)
        {
            //响应自定义按钮点击，根据BtnID完成对应的任务
            XtraMessageBox.Show(System.String.Format("执行自定义按钮任务, BtnID={0}", e.btnID), "CustomButtonClick"
                , System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void axGRPrintViewer1_ToolbarCommandClick(object sender, Axgregn6Lib._IGRPrintViewerEvents_ToolbarCommandClickEvent e)
        { 
        
        }

        private void HealthEduPrint_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (printer == null)  //没设置过打印机 - insert 打印机设置！
            {
                printdefaultsql = @"insert into [BNNursingDocsSystem_Develop].[dbo].[PrintDefault] (FromName,MACAddress,PrinterName) values ('" + Kind.getFileName() + "','" + Kind.GetMacAddress() + "','" + Report.Printer.PrinterName + "')";
                SqlHelper.ExecuteNonQuery(CommandType.Text, printdefaultsql);
            }
            else  //设置过
            {
                if (printer.ToString() == Report.Printer.PrinterName)  //打印机不变
                {
                    //啥也不干
                }
                else //变了 更新
                {
                    printdefaultsql = @"update [BNNursingDocsSystem_Develop].[dbo].[PrintDefault] set  PrinterName ='" + Report.Printer.PrinterName + "' where FromName ='" + Kind.getFileName() + "' and MACAddress ='" + Kind.GetMacAddress() + "'  ";
                    SqlHelper.ExecuteNonQuery(CommandType.Text, printdefaultsql);
                }
            }
        }
    }
}